import React, { Component, Fragment } from "react";
// antd
import { Button, Col, Pagination, Form, Input, Tag, Select, DatePicker } from "antd";
import ProTable from '@ant-design/pro-table';

// http
import http from '../../../utils/request';
const { Option } = Select;


class CloudAppkey extends Component {
    constructor(props){
        super(props);
        this.state = {
            // 表格分页
            total: '',
            page: 1,
            page_number: 10,
            dataSource: [],
            columns: [
                {title: "日期", dataIndex: "date", key: "date", width: "300px"},
                {title: "云平台", dataIndex: "cloud-platform", key: "cloud-platform", width: "300px",
                    render: (text, rowData) => {
                        return(
                            <div>
                                <Tag color="green">{rowData.platform}-{rowData.account_name}</Tag>                                
                            </div>
                        )
                    }
                },
                {title: "appkey", dataIndex: "appkey", key: "appkey", width: "300px"},
                {title: "计费类型", dataIndex: "charge_type", key: "charge_type", width: "300px"},
                {title: "费用", dataIndex: "amount", key: "amount", width: "300px"},
                {title: "更新时间", dataIndex: "updated", key: "updated"},
            ],
            loading: false,
            search_appkey: '',
            search_account_name: null,
            search_platform: null,
            search_charge_type: null,
            search_dates: null,
            search_option_account: [],
            search_option_platform: [],
            search_option_charge_type: [],
        }
        this.formRefAdd = React.createRef();
        this.formRefEdit = React.createRef();
        this.formRefMsg = React.createRef();
    }

    componentDidMount() {
        this.loadData()
        this.loadDataPlatform()
        this.loadDataChangeType()
    }

    loadDataChangeType = () => {
        const requestData = {
            'operation_type': "get_charge_type_list",
        }
        http.patch('/cloud/appkey/billing', {params: requestData}).then(response=>{
            const resp_data = response.data.data
            const search_option_charge_type = []
            for (let i = 0; i < resp_data.length; i++) {
                search_option_charge_type.push(<Option key={resp_data[i]}>{resp_data[i]}</Option>)
            }
            this.setState({
                search_option_charge_type: search_option_charge_type
            })
        }).catch()
    }

    loadDataPlatform = () => {
        const requestData = {
            'page': 1,
            'limit': 1000,
        }
        http.get('/cloud/platform', {params: requestData}).then(response=>{
            const search_option_platform = []
            const resp_data = response.data.data.items
            for (let i = 0; i < resp_data.length; i++) {
                search_option_platform.push(<Option key={resp_data[i].name}>{resp_data[i].name}</Option>)
            }
            this.setState({
                search_option_platform: search_option_platform
            })
        })
    }

    loadData = () => {
        this.setState({
            loading: true
        })
        const requestData = {
            'page': this.state.page,
            'limit': this.state.page_number,
            'appkey__contains': this.state.search_appkey,
            'charge_type': this.state.search_charge_type,
            'date': this.state.search_dates,
            'platform': this.state.search_platform,
            'account_name': this.state.search_account_name,
        }

        // 获取分组列表
        http.get('/cloud/appkey/billing', {params: requestData}).then(response=>{
            this.setState({
                dataSource: response.data.data.items,
                total: response.data.data.pages.total,
                loading: false
            })
        }).catch()
    }

    /** 切换每页数量 */
    onChangeSizePage = (page, page_number) => {
        this.setState({
            page: page,
            page_number: page_number,
        }, () => {
            this.loadData()
        })
    }

    /** 云平台搜索 */
    onFinishSearch = (data) => {
        this.setState({
            page: 1,
            search_appkey: data.appkey ? data.appkey : null,
            search_account_name: data.account_name ? data.account_name : null,
            search_platform: data.platform ? data.platform : null,
        })
        this.loadData()
    }

    // 搜索切换云平台
    onChangePlatform = (value) => {
        if (value) {
            this.setState({
                search_instance_platform: value
            })

            const requestData = {
                'page': 1,
                'limit': 1000,
                'platform': value
            }
    
            http.get('/cloud/account', {params: requestData}).then(response=>{
                const search_option_account = []
                const resp_data = response.data.data.items
                for (let i = 0; i < resp_data.length; i++) {
                    search_option_account.push(<Option key={resp_data[i].name}>{resp_data[i].name}</Option>)
                }
                this.setState({
                    search_option_account: search_option_account
                })
            })

        } else {
            this.setState({
                search_option_account: '',
            })
        }
    }

    // 搜索云账户
    onChangeAccountName = (value) => {
        this.setState({
            search_account_name: value ? value : null
        })
    }

    // 搜索计费类型
    onChangeChargeType = (value) => {
        this.setState({
            search_charge_type: value ? value: null
        })
    }

    onChange = (value, dateString) => {
        this.setState({
            search_dates: dateString ? dateString : null,
        })
    }


    render(){
        const { loading, dataSource, columns, page, total, search_option_platform, search_option_account, search_option_charge_type } = this.state
        const rowSelection = {
            onChange: this.onCheckbox
        }
        var myStyle = {margin: '20px'}
        var myStyle1 = {margin: '10px', float: 'right'}
        return (
            <Fragment>
                <Form layout="inline" onFinish={this.onFinishSearch}>

                    <Form.Item label="日期" name="dates">
                        <DatePicker style={{ width: '200px' }} onChange={this.onChange} />
                    </Form.Item>
                    
                    <Form.Item label="appkey" name="appkey">
                        <Input placeholder="请输入Appkey"  />
                    </Form.Item>

                    <Form.Item label="计费类型" name="charge_type">
                        <Select
                            allowClear
                            showSearch
                            style={{ width: 180 }}
                            placeholder="请选择云账户"
                            onChange={this.onChangeChargeType}
                            >
                            {search_option_charge_type}
                        </Select>                      
                    </Form.Item>

                    <Form.Item label="云平台" name="platform">
                        <Select
                            allowClear
                            showSearch
                            style={{ width: 180 }}
                            placeholder="请选择区域"
                            optionFilterProp="children"
                            onChange={this.onChangePlatform}
                            filterOption={(input, option) =>
                            option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
                            }
                        >
                            {search_option_platform}
                        </Select>
                    </Form.Item>

                    <Form.Item label="云账户" name="account_name">
                        <Select
                            allowClear
                            showSearch
                            style={{ width: 180 }}
                            placeholder="请选择云账户"
                            onChange={this.onChangeAccountName}
                            >
                            {search_option_account}
                        </Select>                      
                    </Form.Item>


                    <Form.Item shouldUpdate={true}>
                        <Button type="primary" htmlType="submit">搜索</Button>
                    </Form.Item>
                </Form>
                
                <div style={myStyle1}></div>
                <div>
                    <ProTable
                        pagination={false}
                        rowSelection={rowSelection} 
                        columns={columns}
                        dataSource={dataSource}
                        rowKey="key"
                        toolBarRender={false}
                        search={false}
                        loading={loading}
                    />
                </div>

                <div style={myStyle}>
                    <Col span={16}>
                        <Pagination
                            onChange={this.onChangeSizePage}
                            current={page}
                            total={total}
                            showSizeChanger
                            showQuickJumper
                            showTotal={total => `Total ${total} items`}
                        />
                    </Col>
                </div>

            </Fragment>
        )
    }
}
export default CloudAppkey;